﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ExitGames.Client.Photon;
using ExitGames.Client.Photon.Lite;
using DICE_GS;

public class SlotInfo
{
	public int index;
	public string nickname;
	public int ready;
	public bool isOccupied;
	public int leaderUnitClassId;
	
	public SlotInfo(int _i, string _nick, int _ready, int leaderId)
	{
		index = _i;
		nickname = _nick;
		ready = _ready;
		isOccupied = true;
		leaderUnitClassId = leaderId;
	}
	
	public void SetOccupy(int r)
	{
		ready = r;
		isOccupied = true;
	}

}

public class RoomWaitingSceneManager : MonoBehaviour
{

    private static Queue<Dictionary<byte, object>> packetQueue = new Queue<Dictionary<byte, object>>();
	public GameObject readyPanelObj;
	public GameObject gameStartBtn;
	public GameObject gameStartDisableBtn;
	private PlayerReadyState readyState;
	public Dictionary<int,SlotInfo> slotInfoDic = new Dictionary<int, SlotInfo>();
	public UILabel hpPotionCount;
	public UILabel evolutionPotionCount;
	
	void Start()
	{
		readyState = readyPanelObj.GetComponent<PlayerReadyState>();
		gameStartBtn.SetActive(false);
		gameStartDisableBtn.SetActive(false);
		
		MyInfo.Instance.SetMyUnitDeckImg();
		
		//show potions count
		hpPotionCount.text = MyInfo.Instance.HPPotion.ToString();
		evolutionPotionCount.text = MyInfo.Instance.ResurrectPotion.ToString();
	}
    // Update is called once per frame
    void Update()
    {
        while (packetQueue.Count > 0)
        {
            Dictionary<byte, object> tmpPacket = packetQueue.Dequeue();

            switch ((byte)tmpPacket[0])
            {
                #region OperationCodes.SC_SHOW_UI:
                case (byte)OperationCodes.SC_SHOW_UI:
                    {
                        PC_SC_SHOW_UI packet = new PC_SC_SHOW_UI(tmpPacket);

                        if (packet.UIType == (byte)UIType.UI_GAME_START)
                        {
                            glog.debug("Show GameStart!!!!");
							gameStartBtn.SetActive(true);
							gameStartDisableBtn.SetActive(false);
                        }
                    }
                    break;
                #endregion
                #region OperationCodes.SC_HIDE_UI:
                case (byte)OperationCodes.SC_HIDE_UI:
                    {
                        PC_SC_HIDE_UI packet = new PC_SC_HIDE_UI(tmpPacket);

                        if (packet.UIType == (byte)UIType.UI_GAME_START)
                        {
                            glog.debug("Hide GameStart!!!!");
							gameStartBtn.SetActive(false);
							gameStartDisableBtn.SetActive(true);
                        }
                    }
                    break;
                #endregion

                #region OperationCodes.OP_CHANGE_READY_STATUS:
                case (byte)OperationCodes.OP_CHANGE_READY_STATUS:
                    {
                    	PC_OP_CHANGE_READY_STATUS packet = new PC_OP_CHANGE_READY_STATUS(tmpPacket);
						
                        if (packet.Status == (byte)ReadyStatus.RS_READY)
                        {
                            //레디중
                            glog.debug("Ready");
							readyState.SetReady(true);
                        }
                        else
                        {
                            //레디아님
                            glog.debug("Not Ready");
							readyState.SetReady(false);
                        }
                    }
                    break;
                #endregion

                #region OperationCodes.OP_LOBBY_STATUS:
                case (byte)OperationCodes.OP_LOBBY_STATUS:
                    {
                        PC_OP_LOBBY_STATUS packet = new PC_OP_LOBBY_STATUS(tmpPacket);
						
						//readyState.ResetState();
                        for (int i = 0; i < packet.UserCount; i++)
                        {
                            glog.debug("ConnectionID :" + packet.ConnectionIDList[i] + " Username: " + packet.UserNameList[i]+" Ready: " + packet.ReadyStatus[i]+" slot: "+packet.SlotInfo[i]);
                            glog.debug("LEADERID : "+packet.LeaderIDLIst[i]);
							//readyState.SetLeaderImg(i, packet.LeaderIDLIst[i]);
							//glog.debug("Username: " + packet.UserNameList[i]);
                            //glog.debug("UserLevel: " + packet.UserLevelList[i]);
                            //glog.debug("LeaderID: " + packet.LeaderIDLIst[i]);
                            //glog.debug("Ready: " + packet.ReadyStatus[i]);
							//readyState.SetState(i, packet.UserNameList[i] , (int)packet.ReadyStatus[i]);
							if( slotInfoDic.ContainsKey(packet.ConnectionIDList[i]))
							{
								glog.debug("containKeys "+packet.ConnectionIDList[i]);
								slotInfoDic[ packet.ConnectionIDList[i] ].SetOccupy((int)packet.ReadyStatus[i]);
							}
							else {
								glog.debug("Not containKeys "+packet.ConnectionIDList[i]);
								string nickForTest = packet.UserNameList[i];// + packet.ConnectionIDList[i];
								slotInfoDic[packet.ConnectionIDList[i]] = new SlotInfo(packet.SlotInfo[i], nickForTest, (int)packet.ReadyStatus[i], packet.LeaderIDLIst[i]);
							}
							
                        }
						
						int keyToDelete = 0;
						bool bDeleteKey = false;
						foreach (KeyValuePair<int, SlotInfo> pair in slotInfoDic)
						{
							if(pair.Value.isOccupied) {
								readyState.SetSlot(pair.Value.index, pair.Value.nickname , pair.Value.ready, pair.Value.leaderUnitClassId);
								glog.debug("occupied setSlot "+pair.Value.index);
							}
							else {
								bDeleteKey = true;
								keyToDelete = pair.Key;
								glog.debug("delete key: "+ keyToDelete+ " clearSlotIndex "+pair.Value.index);
								readyState.ClearSlot(pair.Value.index);
							}
						}
						if(bDeleteKey)
							slotInfoDic.Remove(keyToDelete);
                    }
                    break;
                #endregion
                case (byte)OperationCodes.SC_CHANGE_GAME_STATUS:
                    {
                        PC_SC_CHANGE_GAME_STATUS packet = new PC_SC_CHANGE_GAME_STATUS(tmpPacket);
						if ((GameStatus)packet.Status == GameStatus.GS_PLAYING || (GameStatus)packet.Status == GameStatus.GS_PRE_PLAY)
                        {
                            StageManagerWrapper.m_Connected = true;

                            StageManagerWrapper.Instance.SetStatus((GameStatus)packet.Status);

//							if (CSceneManager.m_Scene != SCENE.SummonMenuScene9)
							{
								CSceneManager c = GameObject.FindObjectOfType(typeof(CSceneManager)) as CSceneManager;
								c.GoToStageScene();
							}
                        }
                    }
                    break;
                default:
                    glog.debug("RoomWaitingSceneManager's Packet Handler Unknown Packet Recieved [" + (byte)tmpPacket[0] + "]");
                    break;
            }
        }
    }

    public static bool PacketHandler(Dictionary<byte, object> _packet)
    {
        glog.debug("RoomWaitingSceneManager Got Packet");
        if (packetQueue != null)
        {
            packetQueue.Enqueue(_packet);
            return true;
        }

        return false;
    }
}
